home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / TTYIO.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  5KB  |  281 lines

  1. #include "jam.h"
  2. /*
  3.  * Name:    Mg 2b
  4.  *        MSDOS terminal I/O (TurboC 1.5)
  5.  *  
  6.  *              Massively modified Nov 1992 for Windows version
  7.  *              Julie A Melbin
  8.  *
  9.  * The functions in this file
  10.  * negotiate with the operating system for
  11.  * keyboard characters, and write characters to
  12.  * the display
  13.  *
  14.  * This version goes along with tty/ibmbios/tty.c.
  15.  * Terminal size is determined there, rather than here.
  16.  */
  17. #include    "def.h"
  18. #include    <stdio.h>
  19. #include    <fcntl.h>
  20. #include    <signal.h>
  21.  
  22. #ifndef WINDOWED
  23. # ifndef CURSES
  24.  void * calloc(size_t, size_t);
  25.  void exit(int);
  26. # endif
  27. #endif
  28.  
  29. static int ttyactivep = FALSE;        /* terminal in editor mode?    */
  30.  
  31. int    nrow = 0;            /* Terminal size, rows.        */
  32. int    ncol = 0;            /* Terminal size, columns.    */
  33.  
  34. /*
  35.  * This function gets called once, to set up
  36.  * the terminal channel.  This is essentially a no-op
  37.  * on msdos, since I/O will all be done via bios calls.
  38.  */
  39. ttopen()
  40. {
  41.   if (!ttyactivep)
  42.     {
  43. #ifndef WINDOWED
  44.       signal(SIGINT, SIG_IGN);
  45.       InitInput();
  46. #endif
  47.       
  48. #ifndef WINDOWED                        /* WinMain sets this up */
  49.       nrow = 25;            /* initial guess */
  50.       ncol = 80;
  51. #endif
  52.       ttyactivep = TRUE;
  53.     }
  54.   return(1);
  55. }
  56.  
  57. ttwindowsize(f,n)
  58. int f,n;
  59. {
  60. #ifdef WINDOWED
  61.   char windowsize[NLINE];
  62.   int s, row, col;
  63.  
  64.   s = eread("New rows,cols:", windowsize, NLINE, EFNEW);
  65.   if (s != TRUE)
  66.     return (s);
  67.  
  68.   sscanf(windowsize, "%d,%d", &row,&col);
  69.   WindowNewSize(row,col);
  70.   return(TRUE);
  71. #else
  72.   ewprintf("NYI");
  73.   return(FALSE);
  74. #endif
  75. }
  76.  
  77. /*
  78.  * This function gets called just
  79.  * before we go back home to the shell. Another
  80.  * MSDOS no_op.
  81.  */
  82. ttclose()
  83. {
  84.   if (ttyactivep)
  85.     ttyactivep = FALSE;
  86.   return(1);
  87. }
  88.  
  89. /********************************************************************/
  90. /* ttputc, ttgetc & typeahead have been deleted from this file, and */
  91. /* moved into the tty specific code.  There is no operating-system  */
  92. /* generic way to do these                                          */
  93. /********************************************************************/
  94.  
  95. /*
  96.  * Flush output.  
  97.  */
  98. ttflush(force)
  99. BOOL force;
  100. {
  101. #ifdef WINDOWED
  102.   WindowFlush();
  103.   if (force)
  104.     WindowSync();
  105. #endif
  106.  
  107. #ifdef CURSES
  108.   wrefresh(stdscr);
  109. #endif
  110.  
  111.   return TRUE;
  112. }
  113.  
  114. BOOL ttfatal()
  115. {
  116. #ifdef WINDOWED
  117.   return (WindowFatalState());
  118. #else
  119.   return (FALSE);
  120. #endif
  121. }
  122.  
  123. #ifndef WINDOWED
  124. # define MAXCHARS 512
  125.  static KCHAR *kbdinput = 0;
  126.  static KCHAR *putback_kbdinput = 0;
  127.  static int kbdcount = 0;
  128.  static int putback_kbdcount = 0;
  129.  
  130. /* alloc the internal event queues
  131. */
  132. void InitInput()
  133. {
  134.  if (!(kbdinput = (KCHAR *)calloc(MAXCHARS, sizeof(KCHAR))))
  135.    printf("Unable to init kbdinput");
  136.  if (!(putback_kbdinput = (KCHAR *)calloc(MAXCHARS, sizeof(KCHAR))))
  137.    printf("Unable to init putback_kbdinput");
  138. }
  139.  
  140. /* putback kchar to to input queue
  141.  */
  142. void PutbackKchar(c)
  143. KCHAR c;
  144. {
  145.   if (putback_kbdcount < MAXCHARS)
  146.     {
  147.       putback_kbdinput[putback_kbdcount] = c;
  148.       putback_kbdcount++;
  149.     }
  150. }
  151.  
  152. /* if putback'ed events, push to normal input stream now
  153. */
  154. void CheckForPutback()
  155. {
  156.   int i, j;
  157.  
  158.   /* if not enough room, die
  159.   */
  160.   if ((MAXCHARS - kbdcount) < putback_kbdcount)
  161.     putback_kbdcount = 0;
  162.   else
  163.     for (j = putback_kbdcount, i = 0; i < j; i++)
  164.        {
  165.          AddKchar(putback_kbdinput[i]);
  166.          putback_kbdcount--;
  167.        }
  168. }
  169.  
  170. /* push normal string to input buf
  171.  */
  172. void AddString(s)
  173. char *s;
  174. {
  175.   while (s && *s)
  176.     AddKchar((KCHAR)*s++);
  177. }
  178.  
  179. /* add kchar to internal queue of collected/manufactured events
  180.  */
  181. void AddKchar(c)
  182. KCHAR c;
  183. {
  184.   if (kbdcount < MAXCHARS)
  185.     {
  186.       kbdinput[kbdcount] = c;
  187.       kbdcount++;
  188.     }
  189.   else
  190.     ttbeep();
  191. }
  192.  
  193. /* return first inqueued char from event into
  194.  * pkchar (if NON_NULL) and return status
  195.  */
  196. BOOL DOSReturnKCHAR(pkchar)
  197. KCHAR *pkchar;
  198. {
  199.   register int i;
  200.   BOOL result = FALSE;
  201.   
  202.   /* if something, remove from queue head 
  203.    * and push the queue down
  204.    * (should use head/tail pointers!)
  205.    */
  206.   if (kbdcount > 0)
  207.     {
  208.       result = TRUE;
  209.       if (pkchar)
  210.     {
  211.       *pkchar = kbdinput[0];
  212.       kbdcount--;
  213.       for (i = 0; i < kbdcount; i++)
  214.         kbdinput[i] = kbdinput[i+1];
  215.     }
  216.     }
  217.   return(result);
  218. }
  219. #endif /* WINDOWED */
  220.  
  221. /* suck up (and throw away if flag set) any pending input
  222. */
  223. void ttflushinput(flag)
  224. int flag;
  225. {
  226.   KCHAR kchar;
  227.  
  228. #ifdef WINDOWED
  229.   while (WindowReturnKCHAR(&kchar))
  230.     if (!flag)
  231.     PutbackKchar(kchar);
  232. #else
  233.   while (DOSReturnKCHAR(&kchar))
  234.     if (!flag)
  235.       PutbackKchar(kchar);
  236. #endif
  237. }
  238.  
  239. /*
  240.  * PANIC:  print error and die, leaving incremental saves
  241.  * and the log file.
  242.  */
  243. void panic(s)
  244. char *s;
  245. {
  246. #ifdef MSW
  247.   char buff[512];
  248.  
  249.   sprintf(buff, "Panic: %s", s);
  250.   WindowMessage(buff, TRUE);
  251.  
  252. #else
  253.  
  254.   fprintf(stderr, "%s\r\n", s);
  255.   exit(1);
  256.  
  257. #endif
  258. }
  259.  
  260. void tteol()
  261. {
  262.   tteeol();
  263. }
  264.  
  265. /*
  266. ** This should check the size of the window, and reset if needed.
  267. */
  268. setttysize()
  269. {
  270. #ifdef WINDOWED 
  271.   WindowGetSize(&nrow, &ncol);
  272. #else
  273.   nrow = 25;
  274.   ncol = 80;
  275. #endif
  276.  return TRUE;
  277. }
  278.  
  279.  
  280.  
  281.